package com.china.school.autoproxycreator;

import com.china.school.interceptor.AfterTest;
import com.china.school.interceptor.AroundTest;
import com.china.school.interceptor.BeforeTest;
import com.china.school.pojo.User;
import org.aspectj.lang.annotation.Around;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;


public class BeanNameAutoProxyCreatorTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeanNameAutoProxyCreatorTest.class);
//        context.register();
        User user = context.getBean(User.class);
        user.eat();
    }

    @Bean
    public User user(){
        return new User();
    }

    @Bean
    public BeforeTest beforeTest(){
        return new BeforeTest();
    }
    @Bean
    public AfterTest afterTest(){
        return new AfterTest();
    }
    @Bean
    public AroundTest aroundTest(){
        return new AroundTest();
    }


    /**
     通过BeanNameAutoProxyCreator可以对批量的Bean进行AOP，并且指定了代理逻辑，指定了一个InterceptorName
     也就是一个Advice，前提条件是这个Advice也得是一个Bean，这样Spring才能找到的，但是BeanNameAutoProxyCreator的缺点很明显，
     它只能根据beanName来指定想要代理的Bean。

     */
    @Bean
    public BeanNameAutoProxyCreator beanNameAutoProxyCreator(){
        BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
        beanNameAutoProxyCreator.setBeanNames("user");
        //设置增强处理
        beanNameAutoProxyCreator.setInterceptorNames("aroundTest","beforeTest","afterTest");

        //设置cglib动态代理
        beanNameAutoProxyCreator.setProxyTargetClass(true);
        return beanNameAutoProxyCreator;
    }

}
